package com.jsmtr.rms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.utils.StringTools;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.xmlbeans.impl.xb.ltgfmt.FileDesc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.ILibTalentService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/pool")
public class LibPoolController {

    @Resource
    private IUtilService utilService;
    @Resource
    private ILibTalentService libTalentService;
    @Resource
    private IDoResumeService doResumeService;

    //[start] 高校校招简历库
    @RequestMapping("/gx")
    public String gxIndex(HttpServletRequest request, Model model) {
        model.addAttribute("resume_type", EnumTools.RecruitType.高校校园招聘.ordinal());
        return "/Library/talentpool/index";
    }
//    @RequestMapping("/gx")
//    public String Index(HttpServletRequest request, Model model) {
//        try {
//            List<ProjectInfo> pros = libTalentService.getProjects(EnumTools.RecruitType.高校校园招聘.ordinal());
//            StringBuilder pross = new StringBuilder("<option value=''>所有招聘方案</option>");
//            for (ProjectInfo projectInfo : pros) {
//                pross.append("<option value='" + projectInfo.getId() + "'>" + projectInfo.getPname() + "</option>");
//            }
//            model.addAttribute("projects", pross);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "/Library/pool/gx";
//    }


    //简历筛选-查询简历数据
    @SuppressWarnings("unchecked")
    @RequestMapping("/getgxresumes")
    public void getresumes(HttpServletRequest request, HttpServletResponse response, DUserResume resume) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom"));
            _map.put("ageto", request.getParameter("ageto"));
            _map.put("workyearfrom", request.getParameter("workyearfrom"));
            _map.put("workyearto", request.getParameter("workyearto"));
            _map.put("heightfrom", request.getParameter("heightfrom"));
            _map.put("heightto", request.getParameter("heightto"));

            Map<String, Object> map = libTalentService.getDResumesMap(page, perpage, _map, resume, "B");
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");
            if (pageCount != 0) {
                /*Object object = map.get("list");
                int num = 1;
                for (HRGXUser dur : (List<HRGXUser>) object) {
                    result.append("<tr><td>" + ((page - 1) * perpage + num) + "</td>");
                    result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + dur.getId() + "'>" + dur.getName() + "</a></td>");
                    result.append("<td>" + dur.getGender() + "</td>");
                    result.append("<td>" + dur.getZzmm() + "</td>");
                    result.append("<td>" + dur.getCode() + "</td>");
                    result.append("<td>" + dur.getUniversity() + "</td>");
                    result.append("<td>" + dur.getMajor() + "</td>");
                    result.append("<td>" + dur.getEducation() + "</td>");
                    result.append("<td>" + dur.getTel() + "</td>");
                    result.append("<td>" + dur.getPname() + "</td>");
                    result.append("<td>" + EnumTools.GXFlowState.values()[libTalentService.getFLow(dur.getFid()).getFid()] + "</td></tr>");
                }*/
                int num = 1;
                List<HashMap> object = (List<HashMap>) map.get("list");
                for (HashMap<String, Object> dur : object) {
                    result.append("<tr><td>" + ((page - 1) * perpage + num) + "</td>");
                    //投简历总次数
                    Integer cardcount = Integer.parseInt(dur.get("CARDCOUNT").toString());
                    if (cardcount > 1) {
                        result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + dur.get("ID") + "'>" + dur.get("NAME") + "</a>&nbsp;<span class='layui-badge'>" + dur.get("CARDCOUNT") + "</span></td>");
                    } else {
                        result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + dur.get("ID") + "'>" + dur.get("NAME") + "</a></td>");
                    }

                    result.append("<td>" + dur.get("SEXNAME") + "</td>");
                    result.append("<td>" + dur.get("ZZMMNAME") + "</td>");
                    result.append("<td>" + dur.get("CARDNO") + "</td>");
                    result.append("<td>" + dur.get("UNIVERSITY") + "</td>");
                    result.append("<td>" + dur.get("MAJORNAME") + "</td>");
                    result.append("<td>" + dur.get("EDUCATIONNAME") + "</td>");
                    result.append("<td>" + dur.get("TEL") + "</td>");
                    result.append("<td>" + dur.get("PRONAME") + "</td>");
                    //result.append("<td>" + EnumTools.GXFlowState.values()[libTalentService.getFLow(dur.get("FID").toString()).getFid()] + "</td></tr>");
                    result.append("<td>" + EnumTools.FlowNomalState.values()[libTalentService.getFLow(dur.get("FID").toString()).getFid()] + "</td></tr>");
                    num++;
                }

            } else {
                result.append("<tr><td colspan='11'>无符合条件的信息</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='11'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }
    }

    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping("/getgxresumesJson")
    public LayuiDataTableResultBean getgxresumesJson(HttpServletRequest request, HttpServletResponse response, DUserResume resume) {
        LayuiDataTableResultBean result = new LayuiDataTableResultBean();
        try {
            int limit = Integer.parseInt(request.getParameter("limit"));
            int page = Integer.parseInt(request.getParameter("page"));
            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom"));
            _map.put("ageto", request.getParameter("ageto"));
            _map.put("workyearfrom", request.getParameter("workyearfrom"));
            _map.put("workyearto", request.getParameter("workyearto"));
            _map.put("heightfrom", request.getParameter("heightfrom"));
            _map.put("heightto", request.getParameter("heightto"));

            Map<String, Object> map = libTalentService.getDResumesMap(page, limit, _map, resume, "B");
            List<HashMap> object = (List<HashMap>) map.get("list");
            JSONArray data = new JSONArray();
            for (HashMap<String, Object> dur :
                    object) {
                JSONObject item = new JSONObject();
                item.put("CARDCOUNT", Integer.parseInt(dur.get("CARDCOUNT").toString()));
                item.put("ID", dur.get("ID"));
                item.put("NAME", dur.get("NAME"));
                item.put("SEXNAME", dur.get("SEXNAME"));
                item.put("ZZMMNAME", dur.get("ZZMMNAME"));
                item.put("CARDNO", dur.get("CARDNO"));
                item.put("UNIVERSITY", dur.get("UNIVERSITY"));
                item.put("MAJORNAME", dur.get("MAJORNAME"));
                item.put("EDUCATIONNAME", dur.get("EDUCATIONNAME"));
                item.put("TEL", dur.get("TEL"));
                item.put("PRONAME", dur.get("PRONAME"));
                item.put("FID", EnumTools.FlowNomalState.values()[libTalentService.getFLow(dur.get("FID").toString()).getFid()]);
                data.add(item);
            }
            result.setCount(0);
            result.setMsg("success");
            result.setCount(((Double) map.get("count")).intValue());
            result.setData(data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCount(-1);
            result.setMsg("err");
            return result;
        }
    }
    //[end]

    //[start] 中专校招简历库
    @RequestMapping("/zz")
    public String ZZCampus(HttpServletRequest request, Model model) {
        try {
            List<ProjectInfo> pros = libTalentService.getProjects(EnumTools.RecruitType.中专校园招聘.ordinal());
            StringBuilder pross = new StringBuilder("<option value=''>所有招聘方案</option>");
            for (ProjectInfo projectInfo : pros) {
                pross.append("<option value='" + projectInfo.getId() + "'>" + projectInfo.getPname() + "</option>");
            }
            model.addAttribute("projects", pross);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/Library/pool/zz";
    }


    //简历筛选-查询简历数据
    @SuppressWarnings("unchecked")
    @RequestMapping("/getzzresumes")
    public void getzzresumes(HttpServletRequest request, HttpServletResponse response, HRZZUser user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom"));
            _map.put("ageto", request.getParameter("ageto"));
            _map.put("workyearfrom", request.getParameter("workyearfrom"));
            _map.put("workyearto", request.getParameter("workyearto"));
            _map.put("heightfrom", request.getParameter("heightfrom"));
            _map.put("heightto", request.getParameter("heightto"));
            Map<String, Object> map = null;
            if (StringTools.trimNotEmpty(user.getPid())) {
                if (user.getPid().toUpperCase().indexOf("E_") >= 0) {
                    DUserResume dUserResume = new DUserResume();
                    dUserResume.setProid(user.getPid());
                    map = libTalentService.getDResumesMap(page, perpage, _map, dUserResume, "E");
                } else {
                    map = libTalentService.getZZResumes(page, perpage, user);
                }

            } else {
                DUserResume dUserResume = new DUserResume();
                map = libTalentService.getZZResumes(page, perpage, user);
            }
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");
            if (pageCount != 0) {
                int num = 1;
                List<HashMap> object = (List<HashMap>) map.get("list");
                for (HashMap<String, Object> dur : object) {
                    result.append("<tr><td>" + ((page - 1) * perpage + num) + "</td>");
                    result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + dur.get("DRID") + "'>" + dur.get("NAME") + "</a></td>");
                    result.append("<td>" + dur.get("GENDER") + "</td>");
                    result.append("<td>" + dur.get("BIRTHDAY") + "</td>");
                    result.append("<td>" + dur.get("CODE") + "</td>");
                    result.append("<td>" + dur.get("ADDUNAME") + "</td>");
                    result.append("<td>" + dur.get("MAJOR") + "</td>");
                    result.append("<td>" + dur.get("EDUCATION") + "</td>");
                    result.append("<td>" + dur.get("TEL") + "</td>");
                    result.append("<td>" + dur.get("PNAME") + "</td>");
                    // result.append("<td>" + EnumTools.ZZFlowState.values()[libTalentService.getFLow(dur.get("FID").toString()).getFid()] + "</td></tr>");
                    result.append("<td>" + EnumTools.FlowNomalState.values()[libTalentService.getFLow(dur.get("FID").toString()).getFid()] + "</td></tr>");
                    num++;
                }
            } else {
                result.append("<tr><td colspan='11'>无符合条件的信息</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='11'>无符合条件的信息</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }
    }

    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping("/getzzresumesJson")
    public LayuiDataTableResultBean getzzresumesJson(HttpServletRequest request, HttpServletResponse response, HRZZUser user) {
        LayuiDataTableResultBean result = new LayuiDataTableResultBean();
        try {
            int limit = Integer.parseInt(request.getParameter("limit"));
            int page = Integer.parseInt(request.getParameter("page"));

            Map<String, Object> _map = new HashMap<String, Object>();
            /*_map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("workyearfrom", request.getParameter("workyearfrom").trim());
            _map.put("workyearto", request.getParameter("workyearto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());*/

            Map<String, Object> map = null;
            if (StringTools.trimNotEmpty(user.getPid())) {
                if ((user.getPid().toUpperCase().indexOf("E_") >= 0) || ((user.getPid().toUpperCase().indexOf("C_") >= 0))){
                    DUserResume dUserResume = new DUserResume();
                    dUserResume.setProid(user.getPid());
                    map = libTalentService.getDResumesMap(page, limit, _map, dUserResume, "E");
                } else {
                    map = libTalentService.getZZResumes(page, limit, user);
                }

            } else {
                DUserResume dUserResume = new DUserResume();
                map = libTalentService.getZZResumes(page, limit, user);
            }

            List<HashMap> object = (List<HashMap>) map.get("list");
            JSONArray data = new JSONArray();
            for (HashMap<String, Object> dur :
                    object) {
                JSONObject item = new JSONObject();
                item.put("DRID", dur.get("DRID"));
                item.put("NAME", dur.get("NAME"));
                item.put("GENDER", dur.get("GENDER"));
                item.put("BIRTHDAY", dur.get("BIRTHDAY"));
                item.put("CODE", dur.get("CODE"));
                item.put("ADDUNAME", dur.get("ADDUNAME"));
                item.put("MAJOR", dur.get("MAJOR"));
                item.put("EDUCATION", dur.get("EDUCATION"));
                item.put("TEL", dur.get("TEL"));
                item.put("PNAME", dur.get("PNAME"));
                item.put("FID", EnumTools.FlowNomalState.values()[libTalentService.getFLow(dur.get("FID").toString()).getFid()]);
                data.add(item);
            }

            result.setCount(0);
            result.setMsg("success");
            result.setCount(((Double) map.get("count")).intValue());
            result.setData(data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCount(-1);
            result.setMsg("err");
            return result;
        }
    }


    //[end]

    //[start] 社会招聘简历库

    @RequestMapping("/sz")
    public String szIndex(HttpServletRequest request, Model model) {
        model.addAttribute("resume_type", EnumTools.RecruitType.社会招聘.ordinal());
        return "/Library/talentpool/index";
    }
//    @RequestMapping("/sz")
//    public String Sz(HttpServletRequest request, Model model) {
//        try {
//            //性别
//            model.addAttribute("xb", utilService.optionList("A", "0"));
//            //名族
//            model.addAttribute("minzu", utilService.optionList("B", "0"));
//            //政治面貌
//            model.addAttribute("zzmm", utilService.optionList("E", "0"));
//            //婚否
//            model.addAttribute("hunfou", utilService.optionList("D", "0"));
//            //最高学历
//            model.addAttribute("education", utilService.optionList("F", "0"));
//            //专业技术职务类型
//            model.addAttribute("posttype", utilService.optionList("I", "0"));
//            //职业资格证
//            model.addAttribute("jndj", utilService.optionList("O", "0"));
//
//            List<ProjectInfo> pros = libTalentService.getProjects(EnumTools.RecruitType.社会招聘.ordinal());
//            StringBuilder pross = new StringBuilder("<option value=''>所有招聘方案</option>");
//            for (ProjectInfo projectInfo : pros) {
//                pross.append("<option value='" + projectInfo.getId() + "'>" + projectInfo.getPname() + "</option>");
//            }
//            model.addAttribute("projects", pross);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "/Library/pool/sz";
//    }


    //简历筛选-查询简历数据
    @SuppressWarnings("unchecked")
    @RequestMapping("/getszresumes")
    public void getszresumes(HttpServletRequest request, HttpServletResponse response, DUserResume resume) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("workyearfrom", request.getParameter("workyearfrom").trim());
            _map.put("workyearto", request.getParameter("workyearto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());

            Map<String, Object> map = libTalentService.getResumes(page, perpage, _map, resume, EnumTools.RecruitType.社会招聘.ordinal());
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");
            if (pageCount != 0) {
                Object object = map.get("list");
                int num = 1;
                for (DUserResume dur : (List<DUserResume>) object) {
                    result.append("<tr><td>" + ((page - 1) * perpage + num) + "</td>");
                    result.append("<td class='link'><a target='_blank' href='" + request.getContextPath() + "/UserResume/ViewDResume?id=" + dur.getId() + "'>" + dur.getName() + "</a></td>");
                    result.append("<td>" + dur.getSexname() + "</td>");
                    result.append("<td>" + dur.getMname() + "</td>");
                    result.append("<td>" + dur.getZzmmname() + "</td>");
                    result.append("<td>" + dur.getCardno() + "</td>");
                    result.append("<td>" + dur.getUniversity() + "</td>");
                    result.append("<td>" + dur.getMajorname() + "</td>");
                    result.append("<td>" + dur.getEducationname() + "</td>");
                    result.append("<td>" + dur.getTel() + "</td>");
                    result.append("<td>" + dur.getJname() + "</td>");
                    result.append("<td>" + EnumTools.FlowState.values()[libTalentService.getFLow(dur.getFid()).getFid()] + "</td></tr>");
                    num++;
                }
            } else {
                result.append("<tr><td colspan='12'>无符合条件的信息</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='12'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }
    }

    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping("/getszresumesJson")
    public LayuiDataTableResultBean getszresumesJson(HttpServletRequest request, HttpServletResponse response, DUserResume resume) {
        LayuiDataTableResultBean result = new LayuiDataTableResultBean();
        try {
            int limit = Integer.parseInt(request.getParameter("limit"));
            int page = Integer.parseInt(request.getParameter("page"));

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("workyearfrom", request.getParameter("workyearfrom").trim());
            _map.put("workyearto", request.getParameter("workyearto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());

            Map<String, Object> map = libTalentService.getResumes(page, limit, _map, resume, EnumTools.RecruitType.社会招聘.ordinal());
            List<DUserResume> object = (List<DUserResume>) map.get("list");
            JSONArray data = new JSONArray();
            for (DUserResume dur :
                    object) {
                JSONObject item = new JSONObject();
                item.put("ID", dur.getId());
                item.put("NAME", dur.getName());
                item.put("SEXNAME", dur.getSexname());
                item.put("MNAME", dur.getMname());
                item.put("ZZMMNAME", dur.getZzmmname());
                item.put("CARDNO", dur.getCardno());
                item.put("UNIVERSITY", dur.getUniversity());
                item.put("MAJORNAME", dur.getMajorname());
                item.put("EDUCATIONNAME", dur.getEducationname());
                item.put("TEL", dur.getTel());
                item.put("JNAME", dur.getJname());
                item.put("FID", EnumTools.FlowNomalState.values()[libTalentService.getFLow(dur.getFid()).getFid()]);
                data.add(item);
            }

            result.setCount(0);
            result.setMsg("success");
            result.setCount(((Double) map.get("count")).intValue());
            result.setData(data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCount(-1);
            result.setMsg("err");
            return result;
        }
    }

    //[end]

}

